<template>
    <div class='returnReason'>
        <div class="container">
            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleShow = 'add'"
                        v-hasPermi="['basic:history:add']">
                        新增</el-button>
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleBulk"
                        v-hasPermi="['customer:manage:add']">
                        批量修改</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight
                :height="tableList.length > 0 ? '900px' : '120px'" @selection-change="handleSelectionChange"
                @row-click="toggleSelection">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="编号" align="center" prop="id" />
                <el-table-column label="原因类型" align="center" prop="type_value" />
                <el-table-column label="排序" align="center" prop="type_value" />
                <el-table-column label="添加时间" align="center" prop="name" />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(statusList, scope.row.status) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit"
                            @click="form = JSON.parse(JSON.stringify(scope.row)); handleShow = 'edit'"
                            v-hasPermi="['basic:history:edit']">
                            编辑
                        </el-button>
                        <el-button v-if="scope.row.status == 0" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['basic:history:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status == 1" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['basic:history:start']">启用
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['basic:history:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- <pagination v-show="pageCount>0" :pageCount="pageCount" :page.sync="queryParams.page" :limit.sync="queryParams.limit"
                @pagination="getList" /> -->
        </div>
        <el-dialog :title="handleShow == 'add' ? '新增' : '编辑'" visible v-if="handleShow" @close="close()" width="40%"
            append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="formLoading">
                <el-col :span="24">
                    <el-form-item label="原因类型" prop="type">
                        <el-input v-model="form.type" placeholder="请输入" />
                        <!-- <el-select v-model="form.type" placeholder="请输入选择" clearable size="small" filterable>
                            <el-option v-for="dict in typeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                        </el-select> -->
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-col :span="24">
                        <el-form-item label="排序" prop="name">
                            <el-input v-model="form.name" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                </el-col>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
// import { getMedicalListsAll, setMedical, setMedicalStatus, delMedical } from "@/api/basic/dataManage.js"
import { mhType, departStatus } from "@/api/dictionary"
import throttle from 'lodash.throttle'
export default {
    name: 'returnReason',
    props: {

    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                // page: 1,
                // limit: 20,
                type: '',
                is_show_hide: 1,
            },
            tableList: [],
            typeList: mhType,
            statusList: departStatus,
            handleShow: "",
            formLoading: false,
            form: {
                id: null,
                name: null,
                type: null,
            },
            rules: {
                type: [{ required: true, message: "请选择", trigger: "change" }],
                name: [{ required: true, message: '请输入病史名称', trigger: 'blur' }],
            },
        }
    },
    created() {
        this.getList()
        console.log(departStatus, "departStatus")
        console.log(mhType, "mhType")
    },
    methods: {
        getList() {
            // this.loading = true;
            // getMedicalListsAll(this.queryParams).then(res => {
            //     this.tableList = res.data;
            //     this.pageCount = res.data.pageCount;
            //     this.loading = false;
            // });
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        close() {
            this.form = {}
            this.handleShow = ""
        },
        toggleSelection(row) {
            this.$refs.tableList.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.selectData = selection;
        },
        handleBulk(ids) {
            this.$confirm('是否确认批量修改?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delSector({ id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("修改成功");
            })
        },
        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将“${row.name}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setMedicalStatus({ medical_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除' + '"' + row.name + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delMedical({ medical_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setMedical(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped></style>
